package com.xsq.rpc.provider.start;

import com.alibaba.fastjson.JSON;
import com.xsq.rpc.common.config.CuratorZookeeperProperties;
import com.xsq.rpc.common.config.RpcConfigProperties;
import com.xsq.rpc.common.config.ServerConfig;
import com.xsq.rpc.common.config.ZookeeperConfigurationCentreProperties;
import com.xsq.rpc.provider.rpc.RpcServer;
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;


/**
 * @ClassName ApplicationRunnerTest
 * @Description TODO
 * @Author xsq
 * @Date 2020/5/30 16:40
 * <p>
 * 在springboot 启动的时候进行初始化,在spring注入bean的后面触发的
 * @throws Exception
 */

@Component
public class ApplicationRunnerTest implements ApplicationRunner {

    @Autowired
    private CuratorZookeeperProperties curatorZookeeperProperties;

    @Autowired
    private RpcConfigProperties rpcConfigProperties;

    @Autowired
    private CuratorFramework getCuratorFramework;

    @Autowired
    private ServerConfig serverConfig;


    /**
     * 在springboot 启动的时候进行初始化
     *
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("远程调用服务端启动serverName：" + rpcConfigProperties.getServerUrl() + "----port：" + rpcConfigProperties.getPort());
        //rpc服务端启动
        RpcServer.startServer(rpcConfigProperties.getServerUrl(), rpcConfigProperties.getPort());
        System.out.println("远程调用服务端启动成功");
        String path = curatorZookeeperProperties.getZkBaseUrl() + curatorZookeeperProperties.getServerName() + "/" + serverConfig.getPort();
        String value = rpcConfigProperties.getServerUrl() + ":" + rpcConfigProperties.getPort();
        System.out.println("zookeeper注册零时节点path：" + path + "------value：" + value);
        createZnode(getCuratorFramework, path, CreateMode.EPHEMERAL, value);
        System.out.println("zookeeper注册零时节点成功");
    }

    /**
     * 把服务端的ip和端口号注册到zookeeper
     */
    public Boolean createZnode(CuratorFramework client, String path, CreateMode createMode, String data) {
        try {
            //当节点路径不存在则创建节点
            if (!checkExists(client, path)) {
                //创建一个节点，并自动递归创建父节点
                client.create().creatingParentsIfNeeded().withMode(createMode).forPath(path, data.getBytes());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 判断节点路径是否存在
     *
     * @param client
     * @param path
     * @return
     */
    public Boolean checkExists(CuratorFramework client, String path) {
        try {
            Stat stat = client.checkExists().forPath(path);
            if (stat == null) {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 修改节点信息
     *
     * @param client
     * @param path
     * @param value
     */
    public void setNodeData(CuratorFramework client, String path, String value) {
        try {
            Stat stat = client.checkExists().forPath(path);
            if (stat != null) {
                client.setData().withVersion(stat.getVersion()).forPath(path, value.getBytes());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

